package com.hxkcloud.nurse.activitys;


import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.adapter.MyCollectAdapter;
import com.hxkcloud.nurse.beans.MyCollectionBean;
import com.hxkcloud.nurse.beans.MyCollectionListBean;
import com.hxkcloud.nurse.beans.resultbeans.ObjectResultBean;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullToRefreshLayout;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullableRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ray_lei on 2016/7/14.
 */
public class MyCollectActivity extends BaseActivity implements View.OnClickListener,PullToRefreshLayout.OnRefreshListener,MyCollectAdapter.ResultSelectedListener {

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;

    private PullToRefreshLayout refreshLayout;
    private PullableRecyclerView recycleView;

    private List<MyCollectionBean> collectList;
    private MyCollectAdapter adapter;

    private ObjectResultBean<MyCollectionListBean> resultBean;

    private LinearLayout emptyLayout;
    private TextView emptyTV;

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_MYCOLLECTIONLISTINFO_SUCCESS:
                if (msg.obj != null) {
                    resultBean = (ObjectResultBean<MyCollectionListBean>) msg.obj;
                    if(resultBean.getData().getCollectionList()!=null){
                        collectList = resultBean.getData().getCollectionList();
                        if(collectList!=null && collectList.size()>0){
                            adapter.setData(collectList);
                            emptyLayout.setVisibility(View.GONE);
                        }else{
                            refreshLayout.setVisibility(View.GONE);
                            emptyLayout.setVisibility(View.VISIBLE);
                        }

                    }


                }

                break;
            case MessageCode.NETWORKCODE_MYCOLLECTIONLISTINFO_FAILURE:
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                }

                break;

        }

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_mycollect);
        collectList = new ArrayList<MyCollectionBean>();
        initView();
//        initData();
        initListener();
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText(getResources().getText(R.string.mycollect_text_title));
        iv_back.setOnClickListener(this);

        refreshLayout = (PullToRefreshLayout) findViewById(R.id.mycollect_refreshlayout);
        recycleView = (PullableRecyclerView) findViewById(R.id.mycollect_recyclerview);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(mLayoutManager);

        adapter = new MyCollectAdapter(this, collectList, recycleView, this,screenWidth);
        recycleView.setAdapter(adapter);

        emptyLayout = (LinearLayout) findViewById(R.id.ll_department_empty);
        emptyTV = (TextView) findViewById(R.id.empty_text);
        emptyTV.setText(getResources().getString(R.string.empty_text_collect));


    }


    @Override
    protected void initListener() {

        iv_back.setOnClickListener(this);
        refreshLayout.setOnRefreshListener(this);

    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    @Override
    protected void initData() {
        YiJiaHuNetWorkUtils.myCollection(handler);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;

        }

    }


    @Override
    public void onSelected(String type, MyCollectionBean info) {
        if("click".equals(type)){
            Intent intent = new Intent(MyCollectActivity.this, ArticleDetailActivity.class);
            intent.putExtra("articleId", info.getCollectionId());
            intent.putExtra("name", info.getTitle());
            intent.putExtra("acctType", info.getType());
            intent.putExtra("isFromCollection","true");
            startActivity(intent);
        }


    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        refreshLayout
                .refreshFinish(PullToRefreshLayout.SUCCEED);
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        refreshLayout
                .loadmoreFinish(PullToRefreshLayout.SUCCEED);
    }
}
